package com.gaswell.aop.aspect;

import com.gaswell.aop.annotation.JHPermission;
import com.gaswell.service.ALLQjService;
import com.gaswell.utils.HttpContextUtils;
import com.gaswell.utils.JwtUtils;
import com.gaswell.vo.Result;
import io.jsonwebtoken.Claims;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

@Aspect
@Component
// 设置优先级，越小，优先级越高
@Order(1)
public class UserInfoAspect {

    @Autowired
    ALLQjService allQjService;


    @Pointcut("@annotation(com.gaswell.aop.annotation.UserInfo)")
    public void userInfoPointCut() {

    }

    @Around(value = "userInfoPointCut()")
    public Object before(ProceedingJoinPoint joinPoint) throws Throwable {
        String depart_name="";
        final Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;

        JHPermission annotation = methodSignature.getMethod().getAnnotation(JHPermission.class);

        final String[] names = methodSignature.getParameterNames();//获取参数的名字
        final Object[] args = joinPoint.getArgs();// 获取参数的值
//      拿到token中存储的departid
        String token = HttpContextUtils.getHeadersValue("token");
        Claims claims = JwtUtils.getClaims(token);
        String depart_id = claims.get("departid", String.class);
//      设置depart
        for (int i = 0; i < args.length; i++) {
            if (names[i].equals("depart")) {
                if (depart_id == "1")
                    depart_name = null;
                else if (depart_id.equals("2"))
                    depart_name = "川渝";
                else
                    depart_name = "大庆";
                args[i]=depart_name;
            }
        }

        return joinPoint.proceed(args);

    }


}